package com.vae.cart.controller;

import com.google.common.collect.Lists;
import com.vae.cart.model.Cart;
import com.vae.cart.service.CartService;
import com.vae.mall.common.response.RespResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车控制层
 */
@RestController
@RequestMapping(value = "/cart")
@CrossOrigin
public class CartController {

    @Autowired
    private CartService cartService;

    /***
     * 添加购物车
     * skuId: skuid
     */
    @GetMapping(value = "/{skuId}/{num}")
    public RespResult add(@PathVariable(value = "skuId")String skuId, @PathVariable(value = "num")Integer num){
        //TODO 还没做登录，先把用户名字写死
        String userName="gp";
        //加入购物车
        cartService.add(skuId,userName,num);
        return RespResult.ok();
    }

    /****
     * 根据用户名查询购物车列表
     * @return
     */
    @GetMapping(value = "/list")
    public RespResult<List<Cart>> list(){
        String userName = "gp";
        List<Cart> carts = cartService.list(userName);
        return RespResult.ok(carts);
    }

    /***
     * 根据购物车ID集合查询购物车列表
     */
    @PostMapping(value = "/list")
    public RespResult<List<Cart>> list(@RequestBody List<String> ids){
        //购物车集合
        List<Cart> carts = Lists.newArrayList(cartService.list(ids));
        return RespResult.ok(carts);
    }
    /***
     * 删除购物车列表
     */
    @DeleteMapping
    public RespResult delete(@RequestBody List<String> ids){
        //删除购物车集合
        cartService.delete(ids);
        return RespResult.ok();
    }




}
